<?php
/**
 * 公用的方法  返回json数据，进行信息的提示
 * @param $status 状态
 * @param string $message 提示信息
 * @param array $data 返回数据
 */
function showMsg($status,$message = '',$data = array()){
    $result = array(
        'status' => $status,
        'message' =>$message,
        'data' =>$data
    );
    return json_encode($result);
}


if (!function_exists('sendCurl')) {

    /**
     * @param $url
     * @param array $body
     * @param array $header
     * @param string $type
     * @return mixed
     * @throws Exception
     */
    function sendCurl($url, $body = [], $type = "GET" , $header = [], $is_proxy = false)
    {
        //1.创建一个curl资源
        $ch = curl_init();
        //2.设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $url);//设置url
        //1)设置请求头
        array_push($header, 'Accept:application/json');
        array_push($header,'Content-Type:application/json');
        array_push($header, 'http:multipart/form-data');
        //设置为false,只会获得响应的正文(true的话会连响应头一并获取到)
        curl_setopt($ch, CURLOPT_HEADER, 0);
        // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        //设置发起连接前的等待时间，如果设置为0，则无限等待。
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        //将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //2)设备请求体
        /*if (count($body) > 0) {
            //$b=json_encode($body,true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));//全部数据使用HTTP协议中的"POST"操作来发送。
        }*/
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
        //设置请求头
        if (count($header) > 0) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        //上传文件相关设置
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);// 从证书中检查SSL加密算
        //3)设置提交方式
        switch (strtoupper($type)) {
            case "GET":
                curl_setopt($ch, CURLOPT_HTTPGET, true);
                break;
            case "POST":
                curl_setopt($ch, CURLOPT_POST, true);
                break;
            case "PUT"://使用一个自定义的请求信息来代替"GET"或"HEAD"作为HTTP请求。这对于执行"DELETE" 或者其他更隐蔽的HTT
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                break;
            case "DELETE":
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
                break;
        }
        //4)在HTTP请求中包含一个"User-Agent: "头的字符串。-----必设

        curl_setopt($ch, CURLOPT_USERAGENT, 'SSTS Browser/1.0');
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');

        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)'); // 模拟用户使用的浏览器
        //5)
        if($is_proxy){
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
            //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);

            curl_setopt($ch, CURLOPT_PROXY, 'http://127.0.0.1:1080');
            //curl_setopt($ch, CURLOPT_PROXYPORT, env('CURL_PROXYPORT'));
        }

        //3.抓取URL并把它传递给浏览器
        $res = curl_exec($ch);
        $result = json_decode($res, true);
        //$httpInfo = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = curl_getinfo($ch);
        $httpCode = $httpInfo['http_code'];
        $err = curl_error($ch);
        //单独日志文件
        /*Log::init(['type' => 'File', 'path' => RUNTIME_PATH . 'rpc/']);
        Log::write($url,'url');
        Log::write(json_encode($body),'参数');
        Log::write($httpCode,'返回状态');
        Log::write($result,'请求结果');
        Log::write($httpInfo,'获取连接资源句柄的信息');*/
        //4.关闭curl资源，并且释放系统资源
        if ($httpCode != 200) {
            if(strpos($err,'timed out') !== false){
                $msg = '请求第三方服务器失败';
                return ['code'=>-1,'result'=> false,'msg'=> $msg];
            }else{
                $msg = $err ? $err : '请求第三方服务器出错';
            }
            if(!empty($result['error'])){
                $msg = $result['error'];
            }
            if(!empty($result['error_description'])){
                $msg = $result['error_description'];
            }
            if(!empty($result['message'])){
                $msg = $result['message'];
            }
            /*if($is_proxy){
                //\Illuminate\Support\Facades\Log::info('代理 sendCurl : ' . $url . $msg);
            }else{
                //\Illuminate\Support\Facades\Log::info('sendCurl : ' . $url . $msg);
            }*/
            return ['msg'=> $msg];
        }
        curl_close($ch);
        return $result ? $result : $res;
    }

}